﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class AquacultureSection_ThreadBreadCrumb : Literal
    {
        protected Queue crumbs = new Queue();
        protected HtmlAnchor anchor;
        public int sectionId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.AquacultureSectionId);
        private int entityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.AquacultureSectionThreadId);
        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);
        private int categoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryId);

        protected override void CreateChildControls()
        {
            string defaultUrl = "";
            if (categoryType == (int)CategoryType.Fishery)
            {
                defaultUrl = SiteUrls.Instance().GetTechFisheryDefaultUrl();
            }
            crumbs.Enqueue(GetAnchor("字典首页", defaultUrl));
            if (sectionId > 0)
            {
                AquacultureSection_Section section = BusinessManager.GetAquacultureSection_Section(sectionId);
                if (section != null)
                {
                    Aquaculture aquaculture = BusinessManager.GetAquaculture(section.AquacultureId.Value);
                    if (aquaculture != null)
                    {
                        AquacultureKebie kebie = BusinessManager.GetAquacultureKebie(aquaculture.KebieId.Value);
                        if (kebie != null)
                        {
                            crumbs.Enqueue(GetAnchor(kebie.Title.Value, SiteUrls.Instance().GetTechAquacultureListUrl(kebie.EntityId.Value,categoryType)));
                        }
                        crumbs.Enqueue(GetAnchor(aquaculture.Title.Value, SiteUrls.Instance().GetTechAquacultureUrl(aquaculture.EntityId.Value, categoryType)));
                    }
                    crumbs.Enqueue(GetAnchor(section.Subject.Value, SiteUrls.Instance().GetAquacultureSection_threadListUrl(sectionId,categoryType,categoryId)));
                }
            }
            else if (entityId > 0)
            {
                AquacultureSection_Thread thread = BusinessManager.GetAquacultureSection_Thread(entityId);
                if (thread != null)
                {
                    AquacultureSection_Section section = BusinessManager.GetAquacultureSection_Section(thread.SectionId.Value);
                    if (section != null)
                    {
                        Aquaculture aquaculture = BusinessManager.GetAquaculture(section.AquacultureId.Value);
                        if (aquaculture != null)
                        {
                            AquacultureKebie kebie = BusinessManager.GetAquacultureKebie(aquaculture.KebieId.Value);
                            if (kebie != null)
                            {
                                crumbs.Enqueue(GetAnchor(kebie.Title.Value, SiteUrls.Instance().GetTechAquacultureUrl(kebie.EntityId.Value, categoryType)));
                            }
                            crumbs.Enqueue(GetAnchor(aquaculture.Title.Value, SiteUrls.Instance().GetTechAquacultureUrl(aquaculture.EntityId.Value, categoryType)));
                        }
                        crumbs.Enqueue(GetAnchor(section.Subject.Value, SiteUrls.Instance().GetAquacultureSection_threadListUrl(section.EntityId.Value,categoryType,categoryId)));
                    }
                    crumbs.Enqueue(GetAnchor(thread.Subject.Value, SiteUrls.Instance().GetAquacultureSection_threadUrl(entityId,categoryType,categoryId)));
                }
            }
        }

        protected Control GetAnchor(string innerText, string href)
        {
            anchor = new HtmlAnchor();
            anchor.InnerHtml = innerText;
            anchor.Attributes["class"] = "crumbAnchor";
            anchor.HRef = href;

            return anchor;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("您的位置:&nbsp;&nbsp;");

            while (crumbs.Count > 0)
            {
                HtmlAnchor a = (HtmlAnchor)crumbs.Dequeue();

                a.Title = a.InnerHtml;

                if ((crumbs.Count > 0) && (a.InnerHtml.Length > 30))
                {
                    a.InnerHtml = a.InnerHtml.Substring(0, 30) + "...";
                }
                a.RenderControl(writer);

                if (crumbs.Count > 0)
                {
                    writer.Write(ResourceManager.GetString("BreadCrumb_Seperator"));
                }
            }

        }

    }
}